/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SFI.Controlador;

import SFI.acceso.Delegado;
import factura.modelo.Articulo;
import factura.modelo.Iva;
import java.util.ArrayList;
import java.util.List;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.ListModelList;

/**
 *
 * @author Romig Sevilla
 * @version 1.0
 */
public class ControladorArticulo{

    String filterArt = "*";
    ListModelList<Articulo> articulos;
    List<ArticuloStatus> articulosVenta;
    Articulo selectedArticulo;
    String mostrarArticulo;
    Double ivaActual=0D;
    Delegado deleg = Delegado.getInstance();
    public List<Articulo> allArticulos = new ArrayList<Articulo>();
    
    public ControladorArticulo() {
        selectedArticulo = new Articulo(5);
        articulosVenta = new ArrayList<ArticuloStatus>();
        


    }

    public Articulo getSelectedArticulo() {
        return selectedArticulo;
    }

    public void setSelectedArticulo(Articulo selectedArticulo) {
        this.selectedArticulo = selectedArticulo;
    }

    public String getMostrarArticulo() {
        return mostrarArticulo;
    }

    public void setMostrarArticulo(String mostrarArticulo) {
        this.mostrarArticulo = mostrarArticulo;
    }

    public ListModelList<Articulo> getArticulos() {
        if (articulos == null) {
            mostrarArticulo();
        }

        return articulos;
    }

    public List<ArticuloStatus> getArticulosVenta() {
        return articulosVenta;
    }

    public void setArticulosVenta(ListModelList<ArticuloStatus> articulosVenta) {
        this.articulosVenta = articulosVenta;
    }

    public void setArticulos(ListModelList<Articulo> articulos) {
        this.articulos = articulos;
    }

    public List<Articulo> getAllArticulos() {
        return allArticulos;
    }

    public void setAllArticulos(List<Articulo> allArticulos) {
        this.allArticulos = allArticulos;
    }

    public String getFilterArt() {
        return filterArt;
    }

    public void setFilterArt(String filterArt) {
        this.filterArt = filterArt;
    }

    @NotifyChange({"selectedArticulo", "articulos", "mostrarArticulo"})
    @Command
    public void mostrarArticulo() {
        mostrarArticulo = "mostrar";
        if (allArticulos.isEmpty()) {
            articulos = new ListModelList<Articulo>();
            allArticulos = this.deleg.buscarTodos(Articulo.class, "descripcion", true, true);
            ivaActual=this.deleg.buscar(Iva.class, 1).getValor();
            articulos.addAll(this.searchArt(filterArt));
            selectedArticulo = null;
        }
        System.out.println("si funcionannnnnn ");
    }

    @NotifyChange("mostrarArticulo")
    @Command
    public void cerrarVentana() {
        mostrarArticulo = null;
    }

    public List<Articulo> searchArt(String fitler) {
        List<Articulo> articulos = new ArrayList<Articulo>();
        for (Articulo art : allArticulos) {
            if ((fitler == null || "*".equals(fitler))
                    || art.getDescripcion().indexOf(fitler) >= 0) {
                articulos.add(art);
            }
        }
        return articulos;
    }
}
